<?php

namespace App\Http\Controllers;

use App\Cate;
use App\GoodImage;
use App\Goods;
use App\GoodsNorm;
use App\GoodsSku;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class GoodsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $where = [];
        if(!empty($request->start)){
            $where[] = ['created_at',">",$request->start];
        }
        if(!empty($request->end)){
            $where[] = ['created_at',"<",$request->end];
        }
        if(!empty($request->name)){
            $where[] = ['name',$request->name];
        }
        $goodses = Goods::where($where)->paginate(2);

        return view("admin.goods.index",compact("goodses", "request"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cates = Cate::all();
        return view("admin.goods.create", compact("cates"));
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Goods $goods)
    {
        //
        $this->validate($request,[
            "cate_id"=>"required|integer",
            "name"=>"required|max:30|min:2",
            "intro"=>"required|min:8",
            "is_on"=>"required|in:0,1",
            "is_new"=>"required|in:0,1",
            "is_best"=>"required|in:0,1",
        ]);
        $goods->cate_id = $request->cate_id;
        $goods->name = $request->name;
        $goods->intro = $request->intro;
        $goods->is_on = $request->is_on;
        $goods->is_new = $request->is_new;
        $goods->is_best = $request->is_best;
        $goods->price = $request->price;
        $res = $goods->save();
        if ($res){
            return [
                "error"=>0,
                "msg"=>"添加成功",
            ];
        }else{
            return [
                "error"=>1,
                "msg"=>"添加失败",
            ];
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(Goods $goods)
    {
        //

        $arr = [];
        foreach($goods->goodsNorms as $goodsNorms){
            $arr[$goodsNorms->norm->name][] = $goodsNorms;
        }

        return view("goods.index", compact("goods","arr"));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function editKucun(Goods $goods)
    {
        return view("admin.goods.editKucun",compact("goods"));
    }

    public function editKucunStore(Request $request)
    {
        foreach($request->field as $k=>$v){
            $arr = explode(",",$v);
            foreach ($arr as $item){
                $goodsNorm = new GoodsNorm();
                $goodsNorm->name = $item;
                $goodsNorm->goods_id = $request->goods_id;
                $goodsNorm->norm_id = $k;
                $goodsNorm->save();
            }
        }
        return [
          "error"=>0,
          "info"=>"添加成功",
        ];
    }

    public function setKucun(Goods $goods)
    {
        foreach($goods->goodsNorms as $goodsNorm){
            $arr[$goodsNorm->norm_id][] = $goodsNorm;
        }
        $norms = $this->dikaer($arr);
        return view("admin.goods.setkucun",compact("norms","goods"));
    }

    public function setKucunStore(Request $request,Goods $goods)
    {
        foreach ($request->norm_id as $k=>$norm_id){
            $goods_skus = new GoodsSku();
            $goods_skus->norm_ids = rtrim($request->norm_id[$k],"|");
            $goods_skus->goods_id = $goods->id;
            $goods_skus->num = $request->num[$k];
            $goods_skus->price = $request->price[$k];
            $goods_skus->save();
        }
        return [
          "error"=>0,
          "info"=>"添加成功",
        ];
    }

    public function image(Goods $goods)
    {
        return view("admin.goods.goodsimg",compact("goods"));
    }

    public function uploadeImage(Goods $goods, Request $request)
    {
        if($request->isMethod('POST')) {
//            var_dump($_FILES);
            $file = $request->file('file');
            //判断文件是否上传成功
            if ($file->isValid()) {
//                //获取原文件名
//                $originalName = $file->getClientOriginalName();
                //扩展名
                $ext = $file->getClientOriginalExtension();
                //文件类型
//                $type = $file->getClientMimeType();
                //临时绝对路径
                $realPath = $file->getRealPath();

                $filename = date('Y-m-d-H-i-S') . '-' . uniqid() . '.' . $ext;

                $bool = Storage::disk('public')->put($filename, file_get_contents($realPath));

                if ($bool){
                    $goodsImage = new GoodImage();
                    $goodsImage->goods_id = $goods ->id;
                    $goodsImage->src = "/storage/".$filename;
                    $goodsImage->save();
                }
                $goods->img = "/storage/".$filename;
                $goods->save();

                return [
                    "code" => 0,
                    "data" => [
                        "src" => "/storage/".$filename
                    ]
                ];
            }
        }
    }
    public function dikaer($arr){
        $arr1 = array();
        $result = array_shift($arr);
        while($arr2 = array_shift($arr)){
            $arr1 = $result;
            $result = array();
            foreach($arr1 as $v){
                foreach($arr2 as $v2){
                    if(!is_array($v))$v = array($v);
                    if(!is_array($v2))$v2 = array($v2);
                    $result[] = array_merge_recursive($v,$v2);
                }
            }
        }
        return $result;
    }

    public function sku(Request $request)
    {
        $sku = $request->sku;

        $goodsSku = GoodsSku::where('norm_ids',$sku)->first();

        if ($goodsSku && $goodsSku->num>0){
            return [
                "error" =>0,
                "goodsSku" => $goodsSku,
            ];
        }
        return [
            "error"=>1,
            "info"=>"无该产品"
        ];

    }

}
